/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SearchSolr;

/**
 *
 * @author ffk
 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class solrSearch {

    protected CommonsHttpSolrServer server;

    public solrSearch() throws Exception {
        server = new CommonsHttpSolrServer(
                "http://localhost:8983/solr");
        server.setParser(new XMLResponseParser());


    }

    public solrSearch(String url) throws Exception {
        server = new CommonsHttpSolrServer(url);
        server.setParser(new XMLResponseParser());

    }

    public String[] SearchTagById(String id) throws Exception{
        SolrQuery query = new SolrQuery();
        query.setQuery("id:" + "tag_"+ id);
        query.setStart(0);
        query.setRows(1);
        QueryResponse response = server.query(query);
        SolrDocumentList documents = response.getResults();
        Iterator<SolrDocument> itr = documents.iterator();
        ArrayList<String[]> result = new ArrayList<String[]>();
        while (itr.hasNext()) {
            SolrDocument doc = itr.next();
            String[] tmp = new String[5];
            tmp[0] = doc.getFieldValue("id").toString().substring(4);
            tmp[1] = doc.getFieldValue("tagname").toString();
            tmp[2] = doc.getFieldValue("date_add").toString();
            tmp[3] = doc.getFieldValue("date_update").toString();
            tmp[4] = doc.getFieldValue("is_public").toString();
            result.add(tmp);
        }
        if(!result.isEmpty())
            return result.get(0);
        return null;
    }

    public ArrayList<String[]> searchTags(String value, int start, int end, int filter) throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("tagname:" + value);
        query.setSortField("id", SolrQuery.ORDER.desc); // new
        query.setStart(start);
        query.setRows(end);
        QueryResponse response = server.query(query);
        SolrDocumentList documents = response.getResults();
        Iterator<SolrDocument> itr = documents.iterator();
        ArrayList<String[]> result = new ArrayList<String[]>();
        while (itr.hasNext()) {
            SolrDocument doc = itr.next();
            String[] tmp = new String[5];
            tmp[0] = doc.getFieldValue("id").toString().substring(4);
            tmp[1] = doc.getFieldValue("tagname").toString();
            tmp[2] = doc.getFieldValue("date_add").toString();
            tmp[3] = doc.getFieldValue("date_update").toString();
            tmp[4] = doc.getFieldValue("is_public").toString();
            if (filter == 1) {
                if (tmp[4].equals("1")) {
                    result.add(tmp);
                }
            } else if (filter == 0) {
                if (tmp[4].equals("0")) {
                    result.add(tmp);
                }
            } else {
                result.add(tmp);
            }

        }

        return result;
    }

    public String[] SearchStatusById(String id) throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("id:" + "status_" + id);
        query.setStart(0);
        query.setRows(1);
        QueryResponse response = server.query(query);
        SolrDocumentList documents = response.getResults();
        Iterator<SolrDocument> itr = documents.iterator();
        ArrayList<String[]> result = new ArrayList<String[]>();
        while (itr.hasNext()) {
            SolrDocument doc = itr.next();
            String[] tmp = new String[6];
            tmp[0] = doc.getFieldValue("id").toString().substring(7);
            tmp[1] = doc.getFieldValue("content").toString();
            tmp[2] = doc.getFieldValue("date_add").toString();
            tmp[3] = doc.getFieldValue("date_update").toString();
            tmp[4] = doc.getFieldValue("is_public").toString();
            Iterator<Object> tag = doc.getFieldValues("tag").iterator();
            String strt = "";
            while(tag.hasNext()){
                String atag = tag.next().toString();
                strt = strt + atag + ",";
            }
            tmp[5] = strt.substring(0, strt.length() - 1);
            result.add(tmp);
        }
        return result.get(0);
    }

    public ArrayList<String[]> searchStatusByTag(String value, int start, int end, int filter) throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("tag:" + value);
        query.setSortField("id", SolrQuery.ORDER.desc); // new
        query.setStart(start);
        query.setRows(end);
        QueryResponse response = server.query(query);
        SolrDocumentList documents = response.getResults();
        Iterator<SolrDocument> itr = documents.iterator();
        ArrayList<String[]> result = new ArrayList<String[]>();
        while (itr.hasNext()) {
            SolrDocument doc = itr.next();
            String[] tmp = new String[6];
            tmp[0] = doc.getFieldValue("id").toString().substring(7);
            tmp[1] = doc.getFieldValue("content").toString();
            tmp[2] = doc.getFieldValue("date_add").toString();
            tmp[3] = doc.getFieldValue("date_update").toString();
            tmp[4] = doc.getFieldValue("is_public").toString();
            Iterator<Object> tag = doc.getFieldValues("tag").iterator();
            String strt = "";
            while(tag.hasNext()){
                String atag = tag.next().toString();
                strt = strt + atag + ",";
            }
            tmp[5] = strt.substring(0, strt.length() - 1);
            if (filter == 1) {
                if (tmp[4].equals("1")) {
                    result.add(tmp);
                }
            } else if (filter == 0) {
                if (tmp[4].equals("0")) {
                    result.add(tmp);
                }
            } else {
                result.add(tmp);
            }
        }
        return result;
    }

    public ArrayList<String[]> searchStatusByContent(String value, int start, int end, int filter) throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("content:" + value);
        query.setSortField("id", SolrQuery.ORDER.desc); // new
        query.setStart(start);
        query.setRows(end);
        QueryResponse response = server.query(query);
        SolrDocumentList documents = response.getResults();
        Iterator<SolrDocument> itr = documents.iterator();
        ArrayList<String[]> result = new ArrayList<String[]>();
        while (itr.hasNext()) {
            SolrDocument doc = itr.next();
            String[] tmp = new String[6];
            tmp[0] = doc.getFieldValue("id").toString().substring(7);
            tmp[1] = doc.getFieldValue("content").toString();
            tmp[2] = doc.getFieldValue("date_add").toString();
            tmp[3] = doc.getFieldValue("date_update").toString();
            tmp[4] = doc.getFieldValue("is_public").toString();
            Iterator<Object> tag = doc.getFieldValues("tag").iterator();
            String strt = "";
            while(tag.hasNext()){
                String atag = tag.next().toString();
                strt = strt + atag + ",";
            }
            tmp[5] = strt.substring(0, strt.length() - 1);
            if (filter == 1) {
                if (tmp[4].equals("1")) {
                    result.add(tmp);
                }
            } else if (filter == 0) {
                if (tmp[4].equals("0")) {
                    result.add(tmp);
                }
            } else {
                result.add(tmp);
            }
        }
        return result;
    }

    public void clearData() throws Exception {
        server.deleteByQuery("*:*");
        server.commit();
    }

    public void insertStatus(String status_id, String content, String date_add, String date_update, String is_public, ArrayList<String> tag) throws Exception {
        server.deleteByQuery("id:" + "status_" + status_id);
        server.commit();
        //---------------------------
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "status_" + status_id);
        doc.addField("content", content);
        doc.addField("date_add", date_add);
        doc.addField("date_update", date_update);
        doc.addField("is_public", is_public);
        for (int i = 0; i < tag.size(); i++) {
            doc.addField("tag", tag.get(i));
        }
        server.add(doc);
        server.commit();
    }

    public void insertTag(String tag_id, String tagname, String date_add, String date_update, String is_public) throws Exception {
        server.deleteByQuery("id:" + "tag_" + tag_id);
        server.commit();
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "tag_" + tag_id);
        doc.addField("tagname", tagname);
        doc.addField("date_add", date_add);
        doc.addField("date_update", date_update);
        doc.addField("is_public", is_public);
        server.add(doc);
        server.commit();
    }

    public void deleteTag(String tag_id) throws Exception {
        server.deleteByQuery("id:" + "tag_" + tag_id);
        server.commit();
    }

    public void deleteStatus(String status_id) throws Exception {
        server.deleteByQuery("id:" + "status_" + status_id);
        server.commit();
    }
}
